using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

public class SceneSave : NetworkBehaviour
{
	public bool AutoSave = true;

	public float SaveInterval = 3f;

	public bool ClearEveryplay;

	private float timeTemp;

	private void Start()
	{
		timeTemp = Time.time;
	}

	public override void OnStartClient()
	{
		LevelIsLoaded();
		base.OnStartClient();
	}

	private void Update()
	{
		if (base.isServer && AutoSave && Time.time > timeTemp + SaveInterval)
		{
			SaveObjectPlacing();
			timeTemp = Time.time;
		}
	}

	public void LevelIsLoaded()
	{
		if (ClearEveryplay)
		{
			ClearObjectPlacing();
		}
		else
		{
			LoadObjectPlacing();
		}
	}

	public void SaveObjectPlacing()
	{
		if (base.isServer)
		{
			ObjectPlacing[] array = (ObjectPlacing[])Object.FindObjectsOfType(typeof(ObjectPlacing));
			string name = SceneManager.GetActiveScene().name;
			string text = string.Empty;
			string text2 = string.Empty;
			string text3 = string.Empty;
			string text4 = string.Empty;
			for (int i = 0; i < array.Length; i++)
			{
				text = text + array[i].ItemID + ",";
				text2 = text2 + array[i].ItemUID + ",";
				string text5 = text3;
				object[] obj = new object[7]
				{
					text5,
					null,
					null,
					null,
					null,
					null,
					null
				};
				Vector3 position = array[i].transform.position;
				obj[1] = position.x;
				obj[2] = ",";
				Vector3 position2 = array[i].transform.position;
				obj[3] = position2.y;
				obj[4] = ",";
				Vector3 position3 = array[i].transform.position;
				obj[5] = position3.z;
				obj[6] = "|";
				text3 = string.Concat(obj);
				text5 = text4;
				object[] obj2 = new object[9]
				{
					text5,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null
				};
				Quaternion rotation = array[i].transform.rotation;
				obj2[1] = rotation.x;
				obj2[2] = ",";
				Quaternion rotation2 = array[i].transform.rotation;
				obj2[3] = rotation2.y;
				obj2[4] = ",";
				Quaternion rotation3 = array[i].transform.rotation;
				obj2[5] = rotation3.z;
				obj2[6] = ",";
				Quaternion rotation4 = array[i].transform.rotation;
				obj2[7] = rotation4.w;
				obj2[8] = "|";
				text4 = string.Concat(obj2);
			}
			PlayerPrefs.SetString(name + "OBJID", text);
			PlayerPrefs.SetString(name + "OBJUID", text2);
			PlayerPrefs.SetString(name + "OBJPOS", text3);
			PlayerPrefs.SetString(name + "OBJROT", text4);
		}
	}

	public void LoadObjectPlacing()
	{
		if (!base.isServer)
		{
			return;
		}
		string name = SceneManager.GetActiveScene().name;
		if (!UnitZ.itemManager)
		{
			return;
		}
		string @string = PlayerPrefs.GetString(name + "OBJID");
		string string2 = PlayerPrefs.GetString(name + "OBJUID");
		string string3 = PlayerPrefs.GetString(name + "OBJPOS");
		string string4 = PlayerPrefs.GetString(name + "OBJROT");
		string[] array = @string.Split(","[0]);
		string[] array2 = string2.Split(","[0]);
		string[] array3 = string3.Split("|"[0]);
		string[] array4 = string4.Split("|"[0]);
		Vector3[] array5 = new Vector3[array.Length];
		Quaternion[] array6 = new Quaternion[array.Length];
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] != string.Empty)
			{
				string[] array7 = array3[i].Split(","[0]);
				if (array7.Length > 2)
				{
					Vector3 zero = Vector3.zero;
					float.TryParse(array7[0], out zero.x);
					float.TryParse(array7[1], out zero.y);
					float.TryParse(array7[2], out zero.z);
					array5[i] = zero;
				}
				string[] array8 = array4[i].Split(","[0]);
				if (array8.Length > 3)
				{
					Quaternion identity = Quaternion.identity;
					float.TryParse(array8[0], out identity.x);
					float.TryParse(array8[1], out identity.y);
					float.TryParse(array8[2], out identity.z);
					float.TryParse(array8[3], out identity.w);
					array6[i] = identity;
				}
				string itemuid = string.Empty;
				if (i < array2.Length && array2.Length > 0)
				{
					itemuid = array2[i];
				}
				UnitZ.itemManager.DirectPlacingObject(array[i], itemuid, array5[i], array6[i]);
			}
		}
	}

	public void ClearObjectPlacing()
	{
		string name = SceneManager.GetActiveScene().name;
		PlayerPrefs.SetString(name + "OBJID", string.Empty);
		PlayerPrefs.SetString(name + "OBJPOS", string.Empty);
		PlayerPrefs.SetString(name + "OBJROT", string.Empty);
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
